@echo off

set "email="
set /a numfiles=0
set /a numfailed=0

if "%1"=="/?" goto :usage
if "%1"=="?" goto :usage
if "%1"=="-?" goto :usage
if "%1"=="--help" goto :usage

REM check if an email address is specified
if "%1" == "-e" (						
	if "%2"=="" ( 
		echo Error: missing email address!
		goto :usage
	)
	REM test to see if there's an @ symbol (simple check to see if it's an email address)
	set testval=%2
	REM remove anything before @
	set testval=%testval:*@=%
	REM limit testval to length 1
	set testval=%testval:~0,1%
	if not "%testval%"=="@" ( 
		echo Error: missing @ in email address!
		goto :usage
	)
	REM set email to address
	set email=%2
	REM get rid of email arguments
	shift
	shift
)

if "%1"=="" goto :usage

REM do-all-in-directory flag specified
if "%1"=="-d" (						
	if "%2"=="" goto :usage
	set directory=%2
	goto :directoryversion
)

REM if list given
if "%1"=="-l" (
	if "%2"=="" goto :usage
	goto :arglistversion
)

REM default mode - list file version:
goto :listversion


:arglistversion
set /a i=1
: loopstart
echo processing file %i% = %~1
run1fsp -e %email% %~1 
set /a numfiles+=1 > NUL
if not %errorlevel% == 0 set /a numfailed+=1
shift
set /a i += 1 > NUL
if "%1"=="" goto : loopend
goto : loopstart
: loopend
goto :endpoint

REM execute all in specified directory
:directoryversion
if not "%directory:~-1%"=="\" set directory=%directory%\
REM dir /B "%directory%"*.fsp
REM if not %errorlevel% == 0 ( 
if not exist "%directory%" (
	set numfiles=?
	goto :direrror
)
echo processing all .fsp files in directory %directory%
for /F %%F in ('dir /B "%directory%"*.fsp') do (
	echo processing file %%F
	echo on
	run1fsp -e %email% %%F
	@echo off
	set /a "numfiles+=1" > NUL
	if not %errorlevel% == 0 set /a "numfailed+=1" > NUL
	echo.
	echo.
)
goto :endpoint

:listversion
REM execute all files listed in the supplied text file
echo Processing all files in the list %~n1%~x1
if not exist "%1" goto :fileerror
for /F "tokens=*" %%G in (%1) do @(
	echo processing file %%G
	if not "%email%"=="" run1fsp -e %email% %%G
	if "%email%"=="" run1fsp %%G
	set /a "numfiles+=1" > NUL
	if not %errorlevel% == 0 set /a "numfailed+=1" > NUL
	echo.
	echo.
)
goto :endpoint

:endpoint
echo Finished processing. %numfailed% of %numfiles% files were processed successfully.
goto :eof

:direrror
echo Error: couldn't find the specified directory, %directory%
goto :eof

:fileerror
echo Error: couldn't find the specified list file, %1
goto :eof

:usage
echo This batch file will process a list of .fsp files using parallel FDTD on 8
echo cores, emailing the success/failure notification of each. Alternatively,
echo all files in a given directory will be processed
echo syntax:
echo     runfsp (-e [email]) [listfile]
echo        runs each of the fsp files listed in [listfile], notifying email
echo        address [email], if specified (otherwise default email is used).
echo     runfsp (-e [email]) -d [directory]
echo		runs all fsp files in the directory [directory], notifying email
echo        address [email], if specified (otherwise default email is used).
echo     runfsp (-e [email]) -l file1.fsp file2.fsp ...
echo		runs the fsp files specified (file1.fsp, file2.fsp etc) notifying
echo        email address [email], if specified (otherwise default email is
echo        used).